package GameEngine.Animation.Programmed;

import GameEngine.Animation.TimeBase;
import GameEngine.helper.PAppletBridge;

public abstract class Animation {
	private TimeBase tb = new TimeBase();
	private int MaxTicks;
	private float MaxValue;
	private float MinValue;
	private int count;
	private boolean isStarted = false; // wurde Animation schon einmal gestartet
										// ?

	public Animation(int maxTicks, int minV, int maxV) {
		tb.reset();
		count = tb.getTicks();

		MaxTicks = maxTicks;
		MinValue = minV;
		MaxValue = maxV;
	}

	public Animation(int maxTicks, float minV, float maxV) {
		tb.reset();
		count = tb.getTicks();

		MaxTicks = maxTicks;
		MinValue = minV;
		MaxValue = maxV;
	}

	public float currentValue() {
		int d = tb.getTicks() - count;
		return (lerp(MinValue, MaxValue, (d / (MaxTicks * 1.0f))));
	}
	
	protected int getTickDifference() {
		return tb.getTicks() - count;
	}

	public float normalizedCurrentValue() {
		int d = tb.getTicks() - count;
		d = d % MaxTicks;
		return (lerp(MinValue, MaxValue, (d / (MaxTicks * 1.0f))));
	}

	public boolean isRunning() {
		return (((tb.getTicks() - count) <= MaxTicks) && isStarted);
	}

	public void start() {
		count = tb.getTicks();
		isStarted = true;
		tb.setPause(false);
	}

	public void stop() {
		isStarted = false;
		tb.setPause(true);
	}

	// Adaptermethoden
	private float lerp(float min, float max, float f) {
		return PAppletBridge.lerp(min, max, f);
	}

	// muss durch abgeleitete Klassen implementiert werden
	public abstract void animate(int x, int y);
}
